package com.gcph.zhuoba.util;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.gcph.zhuoba.main.bean.Desk;
import com.gcph.zhuoba.player.bean.OnlineHelpDesk;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class ImageUtil {

	/**
	 * 截屏
	 * @param activity
	 * @return
	 */
	public static Bitmap captureScreen(Activity activity) {
		// 获取屏幕大小：
		DisplayMetrics metrics = new DisplayMetrics();
		WindowManager WM = (WindowManager) activity
		.getSystemService(Context.WINDOW_SERVICE);
		Display display = WM.getDefaultDisplay();
		display.getMetrics(metrics);
		int height = metrics.heightPixels; // 屏幕高
		int width = metrics.widthPixels; // 屏幕的宽
		// 获取显示方式
		int pixelformat = display.getPixelFormat();
		PixelFormat localPixelFormat1 = new PixelFormat();
		PixelFormat.getPixelFormatInfo(pixelformat, localPixelFormat1);
		int deepth = localPixelFormat1.bytesPerPixel;// 位深
		byte[] piex = new byte[height * width * deepth];

		DataInputStream dStream = null;
		try {
			// 获取fb0数据输入流
			InputStream stream = new FileInputStream(new File("/sdcard/DCIM/Camera/ball_fb_"+System.currentTimeMillis()));
			dStream = new DataInputStream(stream);
			dStream.readFully(piex);
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			if(null != dStream)
			{
				try {
					dStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		// 保存图片
		int[] colors = new int[height * width];
		for (int m = 0; m < colors.length; m++) {
			int r = (piex[m * 4] & 0xFF);
			int g = (piex[m * 4 + 1] & 0xFF);
			int b = (piex[m * 4 + 2] & 0xFF);
			int a = (piex[m * 4 + 3] & 0xFF);
			colors[m] = (a << 24) + (r << 16) + (g << 8) + b;
		}

		// piex生成Bitmap
		Bitmap bitmap = Bitmap.createBitmap(colors, width, height,Bitmap.Config.ARGB_8888);
		L.d("--截屏成功--"+bitmap);
		return bitmap;
	}
	
	/**
	 * 保存图片
	 * @param bitmap
	 * @param bitName
	 * @throws IOException
	 */
	public static File saveBitmap(Bitmap bitmap,String fileName) throws IOException
    {
        File file = new File("/sdcard/DCIM/Camera/"+fileName);
        if(file.exists()){
            file.delete();
        }
        FileOutputStream out = null;
        try{
            out = new FileOutputStream(file);
            if(bitmap.compress(Bitmap.CompressFormat.PNG, 90, out))
            {
                out.flush();
                out.close();
            }
            
            L.d("--截屏图片保存成功--"+file.getPath());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return file;
    }
	
	
	public static Bitmap getViewBitmap(View v) {

		v.clearFocus(); //

		v.setPressed(false); //

		// 能画缓存就返回false

		boolean willNotCache = v.willNotCacheDrawing();

		v.setWillNotCacheDrawing(false);

		int color = v.getDrawingCacheBackgroundColor();

		v.setDrawingCacheBackgroundColor(0);

		if (color != 0) {

			v.destroyDrawingCache();

		}

		v.buildDrawingCache();

		Bitmap cacheBitmap = v.getDrawingCache();

		if (cacheBitmap == null) {

			return null;

		}

		Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

		// Restore the view

		v.destroyDrawingCache();

		v.setWillNotCacheDrawing(willNotCache);

		v.setDrawingCacheBackgroundColor(color);

		return bitmap;

	}

	 public static Bitmap captureScreen2(Activity activity) {

		 activity.getWindow().getDecorView().setDrawingCacheEnabled(true);

		 Bitmap bmp=activity.getWindow().getDecorView().getDrawingCache();

		 return bmp;

	}
	
	/**
	 *  截在教案的桌面屏幕
	 * @param width
	 * @param height
	 * @param fileName
	 * @return
	 */
	public static Bitmap drawBitmapFromDesk(int width, int height, String fileName) {
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		Canvas newCanvas = new Canvas(bitmap);
		Desk.drawSelf(newCanvas, new Paint());

		// 保存截屏图片
		FileOutputStream fos = null;
		try {
			File file = new File("/sdcard/DCIM/Camera/" + fileName);
			fos = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return bitmap;
	}
	
	
	/**
	 * 截在线求助的桌面屏幕
	 * @param width
	 * @param height
	 * @param fileName
	 * @return
	 */
	public static Bitmap drawBitmapFromOnlineHelpDesk(int width, int height, String fileName) {
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		Canvas newCanvas = new Canvas(bitmap);
		OnlineHelpDesk.drawSelf(newCanvas, new Paint());

		// 保存截屏图片
		FileOutputStream fos = null;
		try {
			File file = new File("/sdcard/DCIM/Camera/" + fileName);
			fos = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return bitmap;
	}
}